#include "lightplot2d.h"
#include "settingscaledialog.h"
#include "settinggriddialog.h"
#include "settingcurvedialog.h"
#include "labelseditdialog.h"
#include "legendeditdialog.h"
#include <QtGui>

LightPlot2D::LightPlot2D(QWidget *parent, Qt::WindowFlags f)
    : QFrame(parent, f)
{
    _plotter = new AbstractPlot2D;
    toolBar = new QToolBar;
    currentPointLabel = new QLabel(this);
    _plotter->setCurrentPointLabel(currentPointLabel);
    _plotter->installEventFilter(this);

    createActions();

    toolBar->addWidget(new QWidget(this));
    toolBar->addAction(zoomInAct);
    toolBar->addAction(zoomOutAct);
    toolBar->addAction(changeScaleAct);
    toolBar->addAction(changeGridAct);
    toolBar->addAction(editLabelsAct);
    toolBar->addAction(editCurvesAct);
    toolBar->addAction(editLegendAct);
    toolBar->addSeparator();
    toolBar->addWidget(new QWidget(this));
    toolBar->addWidget(currentPointLabel);

    toolBar->setIconSize(QSize(30, 30));

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(toolBar);
    mainLayout->addWidget(_plotter);
    mainLayout->setSpacing(0);

    loadStyleSheet(":/lightplot2d.css");

    connect(_plotter, SIGNAL(zoomChanged(int, int)), this, SLOT(zoomChanged(int, int)));

}
//-----------------------------------------------------------------------------

void LightPlot2D::createActions()
{
    zoomInAct = new QAction(this);
    zoomInAct->setShortcut(tr("Ctrl++"));
    zoomInAct->setToolTip(tr("Zoom In Ctrl +"));
    zoomInAct->setIcon(QIcon(":/img/zoomin.png"));
    zoomInAct->setText(tr("zoom in"));
    zoomInAct->setEnabled(false);
    connect(zoomInAct, SIGNAL(triggered()), _plotter, SLOT(zoomIn()));

    zoomOutAct = new QAction(this);
    zoomOutAct->setShortcut(tr("Ctrl+-"));
    zoomOutAct->setToolTip(tr("Zoom Out Ctrl -"));
    zoomOutAct->setIcon(QIcon(":/img/zoomout.png"));
    zoomOutAct->setText(tr("zoom out"));
    zoomOutAct->setEnabled(false);
    connect(zoomOutAct, SIGNAL(triggered()), _plotter, SLOT(zoomOut()));

    changeScaleAct = new QAction(this);
    changeScaleAct->setShortcut(tr("Ctrl+S"));
    changeScaleAct->setToolTip(tr("Scale details Ctrl+S"));
    changeScaleAct->setIcon(QIcon(":/img/tuneplot.png"));
    changeScaleAct->setText(tr("scale details"));
    connect(changeScaleAct, SIGNAL(triggered()), this, SLOT(changeScale()));

    changeGridAct = new QAction(this);
    changeGridAct->setShortcut(tr("Ctrl+G"));
    changeGridAct->setToolTip(tr("Grid details Ctrl+G"));
    changeGridAct->setIcon(QIcon(":/img/tuneplot.png"));
    changeGridAct->setText(tr("grid details"));
    connect(changeGridAct, SIGNAL(triggered()), this, SLOT(changeGrid()));

    editLabelsAct = new QAction(this);
    editLabelsAct->setShortcut(tr("Ctrl+A"));
    editLabelsAct->setToolTip(tr("Axis details Ctrl+A"));
    editLabelsAct->setIcon(QIcon(":/img/tuneplot.png"));
    editLabelsAct->setText(tr("axis details"));
    connect(editLabelsAct, SIGNAL(triggered()), this, SLOT(changeAxis()));

    editCurvesAct = new QAction(this);
    editCurvesAct->setShortcut(tr("Ctrl+C"));
    editCurvesAct->setToolTip(tr("Curves details Ctrl+C"));
    editCurvesAct->setIcon(QIcon(":/img/tuneplot.png"));
    editCurvesAct->setText(tr("curves details"));
    connect(editCurvesAct, SIGNAL(triggered()), this, SLOT(changeCurves()));

    editLegendAct = new QAction(this);
    editLegendAct->setShortcut(tr("Ctrl+L"));
    editLegendAct->setToolTip(tr("Legend details Ctrl+L"));
    editLegendAct->setIcon(QIcon(":/img/tuneplot.png"));
    editLegendAct->setText(tr("legend details"));
    connect(editLegendAct, SIGNAL(triggered()), this, SLOT(changeLegend()));

    showLegendAct = new QAction(this);
    showLegendAct->setCheckable(true);
    showLegendAct->setChecked(true);
    showLegendAct->setText(tr("show legend"));
    connect(showLegendAct, SIGNAL(triggered(bool)), _plotter, SLOT(showLegend(bool)));


}
//-----------------------------------------------------------------------------

bool LightPlot2D::loadStyleSheet(const QString &fileName)
{
    QFile file(fileName);
    if (file.open(QFile::ReadOnly)) {
        QString styleSheet = QLatin1String(file.readAll());
        setStyleSheet(styleSheet);
        return true;
    }
    return false;
}
//-----------------------------------------------------------------------------

void LightPlot2D::changeScale()
{
    SettingScaleDialog scaleDialog(this);
    scaleDialog.setPlotter(_plotter);
    scaleDialog.exec();
}
//-----------------------------------------------------------------------------

void LightPlot2D::changeGrid()
{
    SettingGridDialog gridDialog(this);
    gridDialog.setPlotter(_plotter);
    gridDialog.exec();
}
//-----------------------------------------------------------------------------

void LightPlot2D::changeAxis()
{
    LabelsEditDialog axisDialog(this);
    axisDialog.setPlotter(_plotter);
    axisDialog.exec();
}
//-----------------------------------------------------------------------------

void LightPlot2D::changeCurves()
{
    SettingCurveDialog curveDialog(_plotter, this);
    curveDialog.exec();
}
//-----------------------------------------------------------------------------

void LightPlot2D::changeLegend()
{
    LegendEditDialog legendEditDialog(_plotter, this);
    legendEditDialog.exec();
}
//-----------------------------------------------------------------------------

void LightPlot2D::zoomChanged(int zoom, int maxZoom)
{
    if (zoom > 0)
        zoomOutAct->setEnabled(true);
    else if (zoom == 0)
        zoomOutAct->setEnabled(false);
    if (zoom >= maxZoom - 1) {
        zoomInAct->setEnabled(false);
    } else {
        zoomInAct->setEnabled(true);
    }
}
//-----------------------------------------------------------------------------

bool LightPlot2D::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == _plotter) {
        if (event->type() == QEvent::ContextMenu) {
            QContextMenuEvent *cmEvent = static_cast<QContextMenuEvent *>(event);
            QMenu menu(this);
            menu.addAction(zoomInAct);
            menu.addAction(zoomOutAct);
            menu.addAction(changeScaleAct);
            menu.addAction(changeGridAct);
            menu.addAction(editLabelsAct);
            menu.addAction(editCurvesAct);
            menu.addAction(editLegendAct);
            menu.addAction(showLegendAct);
            menu.exec(cmEvent->globalPos());
            return true;
        }
    }
    return QFrame::eventFilter(watched, event);
}
//-----------------------------------------------------------------------------

